package com.ruoyi.web.controller.pay.ma;

import cn.hutool.http.HttpUtil;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * https://7-pay.cn/
 */
public class MaPayUtil {
  public static String KEY = "2T9qTf4rrimldgz2Ns9S28xsZ2tQNdPi";//商户密钥
    /**
     *
     * @param outTradeNo_ 商户单号
     * @param goodsName 物品名称
     * @param price 价格
     * @param payType wxpay,alipay
     */
    public static String pay(String outTradeNo_, String goodsName, String price,String payType, String notifyUrl_) {
        String url = "https://z-pay.cn";//支付地址
        String pid = "20220828133524";//商户id
        String type = payType;//支付类型
        String outTradeNo = outTradeNo_;//商户单号
        String notifyUrl =notifyUrl_;//异步通知
        String returnUrl = "";//跳转地址
        String name = goodsName;//商品名
        String money = price;//价格
        String signType = "MD5";//签名类型



        //参数存入 map
        Map<String, Object> sign = new HashMap<>();
        sign.put("pid",pid);
        sign.put("type",type);
        sign.put("out_trade_no",outTradeNo);
        sign.put("notify_url",notifyUrl);
        sign.put("return_url",returnUrl);
        sign.put("name",name);
        sign.put("money",money);


        String signStr = getSign(sign);



        sign.put("sign_type",signType);
        sign.put("sign",signStr);
        System.out.println("<form id='paying' action='"+url+"/submit.php' method='post'>");
        for(Map.Entry<String,Object> m :sign.entrySet()){
            System.out.println("<input type='hidden' name='"+m.getKey()+"' value='"+m.getValue()+"'/>");
        }
        System.out.println("<input type='submit' value='正在跳转'>");
        System.out.println("</form>");
        System.out.println("<script>document.forms['paying'].submit();</script>");

        String resp=HttpUtil.createPost("https://zpayz.cn/mapi.php")
                .header("Content-Type","application/x-www-form-urlencoded")
                .form(sign).execute().body();
        System.out.println(resp);
        return resp;
    }
    public static String getSign(Map<String, Object> map) {
        //根据key升序排序
        Map<String, Object> signMap = sortByKey(map);
        String signStr = "";

        //遍历map 转成字符串
        for (Map.Entry<String, Object> m : signMap.entrySet()) {
            signStr += m.getKey() + "=" + m.getValue() + "&";
        }

        //去掉最后一个 &
        signStr = signStr.substring(0, signStr.length() - 1);

        //最后拼接上KEY
        signStr += KEY;

        //转为MD5
        signStr = DigestUtils.md5DigestAsHex(signStr.getBytes());
        return signStr;
    }
    public static <K extends Comparable<? super K>, V > Map<K, V> sortByKey(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();

        map.entrySet().stream()
                .sorted(Map.Entry.<K, V>comparingByKey()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        return result;
    }

    public static void main(String[] args) {
        //{"code":1,"msg":"success","trade_no":"1111111","O_id":"3946290","payurl":"https://api.z-pay.cn/pay/wxpay6/pay/O_id/3946290","payurl2":"https://api.z-pay.cn/pay/wxpay6/h5/O_id/3946290","qrcode":"https://api.z-pay.cn/pay/wxpay6/pay/O_id/3946290","img":"https://z-pay.cn/qrcode/aHR0cHM6Ly9hcGkuei1wYXkuY24vcGF5L3d4cGF5Ni9wYXkvT19pZC8zOTQ2Mjkw.jpg"}
        pay("","hu","1","wxpay","222");
    }

}
